package cn.edu.ujn.ch17.controller;

import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;

import com.sun.java.swing.plaf.motif.resources.motif;

import cn.edu.ujn.ch17.dao.Customer;
import cn.edu.ujn.ch17.service.ICustomerService;

@Controller
public class CustomerController {
	@Autowired
	private ICustomerService customerService;
	@RequestMapping("/customer/{id}")
	public String findById(@PathVariable("id") int id,Model model) {
		Customer customer = this.customerService.findById(id);
		model.addAttribute("customer",customer);
		return "customer";		
	}
	@RequestMapping("/toaddCustomer")
	public String toadd() {
		return "addCustomer";
	}
	@RequestMapping("/addCustomer")
	public String add(Customer customer) {
		Customer customer2 = customerService.add(customer);
		customer.setId(customer2.getId());
		System.out.println(customer);
		return "customer";
	}
	@RequestMapping("/list")
	public String findAll(Model model) {
		List<Customer> list = this.customerService.findAll();
		model.addAttribute("list",list);
		return "listCustomer";
	}
}
